[XM] Text wrapping fix, xm create --help_config fix.
authoratse@norwich.uk.xensource.com <atse@norwich.uk.xensource.com>
Fri, 22 Sep 2006 14:06:00 +0000 (15:06 +0100)
committeratse@norwich.uk.xensource.com <atse@norwich.uk.xensource.com>
Fri, 22 Sep 2006 14:06:00 +0000 (15:06 +0100)
* Fix text wrap so it doesn't chop off last word in help message for
  certain cases.
* Fix handling of xm create --help_config
* Remove redundant gopts.usage() call.

Signed-off-by: Alastair Tse <atse@xensource.com>
tools/python/xen/xm/create.py
tools/python/xen/xm/opts.py
tools/python/xen/xm/shutdown.py

index 6194de47a4e0621a70a19f3c8c49e353e52caf69..c60f7eadaade00c86ed871a25da06239f627f0d0 100644 (file)
@@ -56,7 +56,8 @@ gopts.opt('help', short='h',
 
 gopts.opt('help_config',
           fn=set_true, default=0,
-          use="Print help for the configuration script.")
+          use="Print the available configuration variables (vars) for the "
+          "configuration script.")
 
 gopts.opt('quiet', short='q',
           fn=set_true, default=0,
@@ -87,7 +88,7 @@ gopts.opt('config', short='F', val='FILE',
           use="Domain configuration to use (SXP).\n"
           "SXP is the underlying configuration format used by Xen.\n"
           "SXP configurations can be hand-written or generated from Python "
-          "configuration scripts, using the -n (dryrun) option to print\n"
+          "configuration scripts, using the -n (dryrun) option to print "
           "the configuration.")
 
 gopts.opt('dryrun', short='n',
@@ -1014,11 +1015,10 @@ def get_xauthority():
 def parseCommandLine(argv):
     gopts.reset()
     args = gopts.parse(argv)
-    if gopts.vals.help:
-        gopts.usage()
-    if gopts.vals.help or gopts.vals.help_config:
-        gopts.load_defconfig(help=1)
+
     if gopts.vals.help or gopts.vals.help_config:
+        if gopts.vals.help_config:
+            print gopts.val_usage()
         return (None, None)
 
     if not gopts.vals.display:
index e0b650bbae0aea3fc0cf7237a5157fff9d084826..189875d55e806ceed90dd09210fe14f343d0f1db 100644 (file)
@@ -24,36 +24,32 @@ import os.path
 import sys
 import types
 
+def _line_wrap(text, width = 70):
+    lines = []
+    current_line = ''
+    words = text.strip().split()
+    while words:
+        word = words.pop(0)
+        if len(current_line) + len(word) + 1 < width:
+            current_line += word + ' '
+        else:
+            lines.append(current_line.strip())
+            current_line = word + ' '
+            
+    if current_line:
+        lines.append(current_line.strip())
+    return lines
+
 def wrap(text, width = 70):
     """ Really basic textwrap. Useful because textwrap is not available
     for Python 2.2, and textwrap.wrap ignores newlines in Python 2.3+.
     """
-    import string
-    
     if len(text) < width:
         return [text]
     
     lines = []
     for line in text.split('\n'):
-        line = line.strip()
-        if len(line) < width:
-            lines.append(line)
-            continue
-        
-        pos = 0
-        while pos <= len(line):
-            wline = line[pos:pos+width].strip()
-            if len(wline) < 2:
-                break
-            
-            if wline[-1] in tuple(string.punctuation):
-                pos += width
-            else:
-                lastword = wline.split()[-1]
-                wline = wline[:-len(lastword)]
-                pos += width - len(lastword)
-            lines.append(wline)
-                
+        lines += _line_wrap(line, width)
     return lines
 
 class OptionError(Exception):
@@ -299,18 +295,22 @@ class Opts:
 
     def __str__(self):
         options = [s for s in self.options if s.optkeys[0][0] == '-']
-        optvals = [s for s in self.options if s.optkeys[0][0] != '-']
         output = ''
         if options:
             output += '\nOptions:\n\n'
             output += '\n'.join([str(o) for o in options])
             output += '\n'
+        return output
+
+    def val_usage(self):
+        optvals = [s for s in self.options if s.optkeys[0][0] != '-']
+        output = ''
         if optvals:
             output += '\nValues:\n\n'
             output += '\n'.join([str(o) for o in optvals])
             output += '\n'
         return output
-
+    
     def opt(self, name, **args):
         """Add an option.
 
index 1fe7fbede5fc2e79951f5de15dc2ffb253795588..a63c3523cab36bccf1b013f16f7cc7e1f7ca3181 100644 (file)
@@ -120,7 +120,6 @@ def main(argv):
     opts = gopts
     args = opts.parse(argv)
     if opts.vals.help:
-        opts.usage()
         return
     if opts.vals.all:
         main_all(opts, args)